package clw.toy;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 * @author chengliwei
 */
public class NativeInstance {
    Class clazz;
    Object instance;

    NativeInstance(Class clazz) {
        this.clazz = clazz;
        try {
            Constructor constructor = clazz.getConstructor();
            instance = constructor.newInstance();
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    NativeInstance(Object instance) {
        this.instance = instance;
    }

    public Object get(String name) {
        return new NativeFunction(instance, name);
    }

    @Override
    public String toString() {
        return instance.toString();
    }
}
